package com.own.component.common.login.config;

import com.own.component.common.login.AuthenticationLoginUser;
import com.own.component.common.login.parent.ParentAuthenticationDefaultCacheLoginUser;
import com.own.component.common.login.parent.ParentAuthenticationDefaultLoginUser;
import com.own.component.common.login.parent.ParentAuthenticationLoginUser;
import com.own.component.common.login.property.AuthenticationLoginUserProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * DefaultNoAuthenticationLoginUser
 *
 * @author chenxueli
 * @date 2023-08-10 16:16:34
 */
@Component
public class AutoParentAuthenticationLoginUser {

    @Resource
    private AuthenticationLoginUserProperty authenticationLoginUserProperty;
    @Resource
    private AuthenticationLoginUser authenticationLoginUser;
    @Resource
    private HttpServletRequest request;

    @Bean
    @ConditionalOnMissingBean(AuthenticationLoginUser.class)
    public AuthenticationLoginUser authenticationLoginUser() {
        return new DefaultNoAuthenticationLoginUser();
    }

    @Bean
    @ConditionalOnMissingBean(ParentAuthenticationLoginUser.class)
    public ParentAuthenticationLoginUser parentAuthenticationLoginUser() {
        if (authenticationLoginUserProperty.isCacheSessionUser()) {
            return new ParentAuthenticationDefaultCacheLoginUser(authenticationLoginUser, request);
        }
        return new ParentAuthenticationDefaultLoginUser(authenticationLoginUser);
    }

}
